﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class DropZone : System.Web.UI.Page
    {
        TARSDelivery_EprojectEntities db = new TARSDelivery_EprojectEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataToGrid();

                //Add item to cboFilterBranchesZone
                RadComboBoxItem comboxboxitem = new RadComboBoxItem();
                comboxboxitem.Text = "All Branches Zone";
                comboxboxitem.Value = "All";
                cboFilterBranchesZone.Items.Add(comboxboxitem);

                var getBranchesZone = db.t_BrachesZone.Where(x => x.Active == true);
                foreach (var get in getBranchesZone)
                {
                    comboxboxitem = new RadComboBoxItem();
                    comboxboxitem.Text = get.ZoneName;
                    comboxboxitem.Value = get.ID.ToString();
                    cboFilterBranchesZone.Items.Add(comboxboxitem);
                }
            }
            else
            {
                var getDropZone = db.v_DropZone.OrderByDescending(x => x.ZoneName).OrderByDescending(x => x.CarrierTypeName);
                RadGrid1.DataSource = getDropZone.ToList();
            }
        }

        //Load Data To Grid With Order By
        private void LoadDataToGrid()
        {
            var getDropZone = db.v_DropZone.OrderByDescending(x => x.ZoneName).OrderByDescending(x => x.CarrierTypeName);
            RadGrid1.DataSource = getDropZone.ToList();
            RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                //Set Visible in Insert & Update Item
                GridEditFormItem edit = (GridEditFormItem)e.Item;
                edit["NewID"].Visible = false;
                edit["CreatedTime"].Visible = false;

                //Set style in Update Item
                if (!(e.Item is IGridInsertItem))
                {

                    GridEditableItem item = (GridEditableItem)e.Item;
                    RadComboBox cboBranchesZone = (RadComboBox)item.FindControl("cboZoneName");
                    RadComboBox cboCarrierType = (RadComboBox)item.FindControl("cboCarrierType");

                    //Set selected item combobox Branches Zone
                    string getzonename = ((v_DropZone)e.Item.DataItem).ZoneName;
                    int AutoCount = 0;
                    int GetIndex = 0;
                    foreach (RadComboBoxItem getitem in cboBranchesZone.Items)
                    {
                        if (getitem.Text == getzonename)
                        {
                            GetIndex = AutoCount;
                        }
                        AutoCount++;
                    }
                    cboBranchesZone.SelectedIndex = GetIndex;
                    Guid getBranchesZoneID = new Guid(cboBranchesZone.SelectedItem.Value.ToString());

                    //Fill data to combobox Carrier Type
                    var getCarrierType = db.t_CarrierType.Where(x => x.BranchesZoneID == getBranchesZoneID && x.Active == true);
                    cboCarrierType.DataSource = getCarrierType.ToList();
                    cboCarrierType.DataTextField = "CarrierTypeName";
                    cboCarrierType.DataValueField = "ID";
                    cboCarrierType.DataBind();

                    //Set selected item combobox Carrier Type
                    string getCarrierTypeName = ((v_DropZone)e.Item.DataItem).CarrierTypeName;
                    AutoCount = 0;
                    GetIndex = 0;
                    foreach (RadComboBoxItem getitem in cboCarrierType.Items)
                    {
                        if (getitem.Text == getCarrierTypeName)
                        {
                            GetIndex = AutoCount;
                        }
                        AutoCount++;
                    }
                    cboCarrierType.SelectedIndex = GetIndex;

                    Session["BranchesZoneID"] = cboBranchesZone.SelectedItem.Value;
                    Session["CarrierTypeID"] = cboCarrierType.SelectedItem.Value;
                }

                //Set style in Insert Item
                else
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    RadComboBox cboBranchesZone = (RadComboBox)item.FindControl("cboZoneName");
                    RadComboBox cboCarrierType = (RadComboBox)item.FindControl("cboCarrierType");
                    Guid getBranchesZoneID = new Guid(cboBranchesZone.Items[0].Value.ToString());
                    var getCarrierType = db.t_CarrierType.Where(x => x.BranchesZoneID == getBranchesZoneID && x.Active == true);
                    cboCarrierType.DataSource = getCarrierType.ToList();
                    cboCarrierType.DataTextField = "CarrierTypeName";
                    cboCarrierType.DataValueField = "ID";
                    cboCarrierType.DataBind();
                    Session["BranchesZoneID"] = cboBranchesZone.Items[0].Value;
                    Session["CarrierTypeID"] = cboCarrierType.Items[0].Value;
                }
            }


        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            //Insert New Item
            GridEditableItem item = e.Item as GridEditableItem;
            String DropZoneName = (item["DropZoneName"].Controls[0] as TextBox).Text;
            String DropZoneID = (item["NewID"].Controls[0] as TextBox).Text;
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid newDropZoneID = new Guid(DropZoneID);
            Guid CarrierTypeID = new Guid(Session["CarrierTypeID"].ToString());

            t_DropZone insertItem = new t_DropZone();
            insertItem.ID = newDropZoneID;
            insertItem.DropZoneName = DropZoneName;
            insertItem.CarrierTypeID = CarrierTypeID;
            insertItem.Active = Active;
            insertItem.CreatedTime = DateTime.Now;
            db.t_DropZone.Add(insertItem);
            db.SaveChanges();

            LoadDataToGrid();

            //Reset Grid
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearSelectedItems();
            RadGrid1.MasterTableView.ClearChildEditItems();

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            //Update item
            GridEditableItem item = e.Item as GridEditableItem;
            String DropZoneName = (item["DropZoneName"].Controls[0] as TextBox).Text;
            String DropZoneID = (item["NewID"].Controls[0] as TextBox).Text;
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid newDropZoneID = new Guid(DropZoneID);
            Guid CarrierTypeID = new Guid(Session["CarrierTypeID"].ToString());

            db = new TARSDelivery_EprojectEntities();
            t_DropZone editDropZone = db.t_DropZone.First(x => x.ID == newDropZoneID && x.Active == true);
            editDropZone.DropZoneName = DropZoneName;
            editDropZone.CarrierTypeID = CarrierTypeID;
            editDropZone.Active = Active;
            db.SaveChanges();

            LoadDataToGrid();

            //Reset RadGrid
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearChildEditItems();
            RadGrid1.MasterTableView.ClearSelectedItems();
        }



        protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["BranchesZoneID"] = e.Value;
            RadComboBox combo1 = (RadComboBox)sender;
            GridEditFormItem editItem = (GridEditFormItem)combo1.NamingContainer;
            Guid getBranchesZoneID = new Guid(e.Value.ToString());

            RadComboBox cboCarrierType = (RadComboBox)editItem.FindControl("cboCarrierType");
            cboCarrierType.Items.Clear();
            var getCarrierType = db.t_CarrierType.Where(x => x.BranchesZoneID == getBranchesZoneID && x.Active == true);
            cboCarrierType.DataSource = getCarrierType.ToList();
            cboCarrierType.DataTextField = "CarrierTypeName";
            cboCarrierType.DataValueField = "ID";
            cboCarrierType.DataBind();
            Session["CarrierTypeID"] = cboCarrierType.Items[0].Value;
        }

        protected void RadComboBox2_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["CarrierTypeID"] = e.Value;
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
            {
                RadGrid1.MasterTableView.IsItemInserted = false;
            }
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                RadGrid1.MasterTableView.ClearEditItems();
                Guid newguid = Guid.NewGuid();
                e.Canceled = true;
                Hashtable defaultvalues = new Hashtable();
                defaultvalues["NewID"] = newguid.ToString();
                defaultvalues["CreatedTime"] = DateTime.Now;
                defaultvalues["Active"] = true;
                e.Item.OwnerTableView.InsertItem(defaultvalues);

            }
        }

        protected void cboFilterBranchesZone_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (e.Value != "All")
            {
                cboFilterCarrierTypeName.Enabled = true;
                var getDropZone = db.v_DropZone.Where(x => x.ZoneName == e.Text.Trim()).OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = getDropZone.ToList();
                RadGrid1.Rebind();

                //Add item to cboCarrierType
                if (cboFilterCarrierTypeName.Items.Count > 0)
                    cboFilterCarrierTypeName.Items.Clear();

                Guid BranchesZoneID = new Guid(e.Value.ToString());
                var getCarrierType = db.t_CarrierType.Where(x => x.BranchesZoneID == BranchesZoneID && x.Active == true);

                RadComboBoxItem comboxboxitem = new RadComboBoxItem();
                comboxboxitem.Text = "All Carrier Type";
                comboxboxitem.Value = "All";
                cboFilterCarrierTypeName.Items.Add(comboxboxitem);
                foreach (var get in getCarrierType)
                {
                    comboxboxitem = new RadComboBoxItem();
                    comboxboxitem.Text = get.CarrierTypeName;
                    comboxboxitem.Value = get.ID.ToString();
                    cboFilterCarrierTypeName.Items.Add(comboxboxitem);
                }
                cboFilterCarrierTypeName.SelectedIndex = 0;
            }
            else
            {
                cboFilterCarrierTypeName.Enabled = false;
                cboFilterCarrierTypeName.Items.Clear();
                cboFilterCarrierTypeName.Text = "";
                LoadDataToGrid();
            }
        }

        protected void cboFilterCarrierTypeName_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (e.Value != "All")
            {
                var getDropZone = db.v_DropZone.Where(x => x.CarrierTypeName == e.Text.Trim()).OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = getDropZone.ToList();
                RadGrid1.Rebind();
            }
            else
            {
                var getDropZone = db.v_DropZone.Where(x => x.ZoneName == cboFilterBranchesZone.SelectedItem.Text.Trim()).OrderByDescending(x => x.ZoneName).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = getDropZone.ToList();
                RadGrid1.Rebind();
            }
        }
    }
}